package com.baomidou.springwind.controller.supply;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.kisso.annotation.Permission;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.springwind.common.business.network;
import com.baomidou.springwind.controller.BaseController;
import com.baomidou.springwind.entity.*;
import com.baomidou.springwind.pojo.SiteSelection;
import com.baomidou.springwind.service.NetworkResponseService;
import com.baomidou.springwind.service.NetworkSupplyService;
import com.baomidou.springwind.mapper.NetworkSupplyDao;
import com.baomidou.springwind.mapper.NetworkDepotDao;
import com.baomidou.springwind.mapper.NetworkDepotGoodsDao;
import com.baomidou.springwind.mapper.NetworkReliabilityDao;
import com.baomidou.springwind.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.*;

/**
 * <p>
 * 物流网络规划反应性
 * </p>
 *
 * @Author huangtao
 * @Date 2017-8-21
 */
@Controller
@RequestMapping("/network/supply")
public class ResponseController extends BaseController {

    @Autowired
    private NetworkSupplyService networkSupplyService;

    @Autowired
    private NetworkSupplyDao networkSupplyDao;

    @Autowired
    private ProjectService projectService;

    @Autowired
    private NetworkResponseService networkResponseService;


    @Autowired
    private NetworkReliabilityDao networkReliabilityDao;

    @Autowired
    private NetworkDepotDao networkDepotDao;

    @Autowired
    private NetworkDepotGoodsDao networkDepotGoodsDao;

    /**
     * 反应性
     */
    @RequestMapping("/response")
    @Permission("1001")
    public String response(Model model){
        EntityWrapper<NetworkDepot> wrapper = new EntityWrapper<NetworkDepot>();
        wrapper.eq("batch_id",getProjectId());
        wrapper.eq("depot_level", 1);
        model.addAttribute("prjList", networkDepotDao.selectList(wrapper));
        return pjaxPath("/lpp/network/supply/response/main", model);
    }

    /**
     * 反应性数据展示
     * @return
     */
    @ResponseBody
    @RequestMapping("/response/getDataMap")
    @Permission("1001")
    public String getData() {
        List<NetworkDepot> list0=getMap1();
        Map<SiteSelection, List<SiteSelection>> map0=buildMap1(list0);
        JSONArray jsonArray0 = network.buildMap(map0);//地图1的json数据
        return callbackSuccess(jsonArray0);
    }

    /**
     * 反应性数据展示
     * @return
     */
    @ResponseBody
    @RequestMapping("/response/getAllData")
    @Permission("1001")
    public String getAllData() {
        Random random=new Random(5);
        JSONObject ret=new JSONObject();
        EntityWrapper<NetworkDepot> wrapper1 = new EntityWrapper<NetworkDepot>();
        wrapper1.eq("batch_id", getProjectId());
        wrapper1.eq("depot_level",1);
        List<NetworkDepot> list1= networkDepotDao.selectList(wrapper1);
        EntityWrapper<NetworkDepot> wrapper2 = new EntityWrapper<NetworkDepot>();
        wrapper2.eq("batch_id", getProjectId());
        wrapper2.eq("depot_level",2);
        List<NetworkDepot> list2= networkDepotDao.selectList(wrapper2);
        JSONArray array1=new JSONArray();
        JSONArray array2=new JSONArray();
        JSONArray array3=new JSONArray();
        JSONArray array4=new JSONArray();
        JSONArray array5=new JSONArray();
        JSONArray array6=new JSONArray();
        JSONArray array7=new JSONArray();
        JSONArray array8=new JSONArray();
        String str1="";
        int distance1=0;
        for (int i=0;i<5;i++){
            for (int j=0;j<2;j++){
                str1=list1.get(i).getDepotName()+"-"+list2.get(j).getDepotName();
                distance1=Math.abs(random.nextInt(30)*100);
                array1.add(str1);
                array2.add(distance1);
            }
        }
        for(int i=0;i<list1.size();i++){
            array3.add(list1.get(i).getDepotName());
            array4.add(Math.abs(random.nextInt(40)*100));
        }
        for(int i=0;i<list1.size();i++){
            array5.add(list1.get(i).getDepotName());
            array6.add(Math.abs(random.nextInt(100)*0.1));
        }
        for(int i=0;i<list1.size();i++){
            array7.add(list1.get(i).getDepotName());
            array8.add(Math.abs(random.nextInt(10000)));
        }

        NetworkResponse response=new NetworkResponse();
        JSONObject obj;

        obj=new JSONObject();
        obj.put("arr1",array1);
        obj.put("arr2",array2);
        response.setHaulDistance(obj.toJSONString());

        obj=new JSONObject();
        obj.put("arr3",array3);
        obj.put("arr4",array4);
        response.setOrderPoint(obj.toJSONString());

        obj=new JSONObject();
        obj.put("arr5",array5);
        obj.put("arr6",array6);
        response.setTotalDistance(obj.toJSONString());

        obj=new JSONObject();
        obj.put("arr7",array7);
        obj.put("arr8",array8);
        response.setEoq(obj.toJSONString());

       networkResponseService.insert(response);


        ret.put("arr1",array1);
        ret.put("arr2",array2);
        ret.put("arr3",array3);
        ret.put("arr4",array4);
        ret.put("arr5",array5);
        ret.put("arr6",array6);
        ret.put("arr7",array7);
        ret.put("arr8",array8);
        return callbackSuccess(ret);
    }

    /**
     * 返回该id下的仓库信息
     * @return
     */
    public List<NetworkDepot> getMap1(){
        EntityWrapper<NetworkDepot> wrapper = new EntityWrapper<NetworkDepot>();
        wrapper.eq("batch_id", getProjectId());
        List<NetworkDepot> list = networkDepotDao.selectList(wrapper);
        return list;
    }

    /**
     * 通过list0生成对应的关系
     * @param list0
     * @return
     */
    public Map<SiteSelection, List<SiteSelection>> buildMap1(List<NetworkDepot> list0){
        Map<SiteSelection, List<SiteSelection>> map0=new HashMap<SiteSelection, List<SiteSelection>>();
        SiteSelection s1;
        for(int i=0;i<list0.size();i++){
            if(list0.get(i).getDepotLevel()==1){
                s1=new SiteSelection();
                s1.setName(list0.get(i).getDepotName());
                s1.setOrder(new BigDecimal(100));
                s1.setCoordinateX(new BigDecimal(list0.get(i).getDepotLng()));
                s1.setCoordinateY(new BigDecimal(list0.get(i).getDepotLat()));
                s1.setLnglatId(list0.get(i).getId());
                map0.put(s1,null);
            }
        }
        List<SiteSelection> selections0;
        SiteSelection selection0;
        for (Map.Entry<SiteSelection, List<SiteSelection>> entry:map0.entrySet()){
            long pId=entry.getKey().getLnglatId();
            selections0=new ArrayList<SiteSelection>();
            for(int i=0;i<list0.size();i++){
                if(list0.get(i).getpId()!=null&&list0.get(i).getpId()==pId){
                    selection0=new SiteSelection();
                    selection0.setName(list0.get(i).getDepotName());
                    selection0.setCoordinateX(new BigDecimal(list0.get(i).getDepotLng()));
                    selection0.setCoordinateY(new BigDecimal(list0.get(i).getDepotLat()));
                    selection0.setOrder(new BigDecimal(100));
                    selections0.add(selection0);
                }
            }
            entry.setValue(selections0);
        }
        return map0;
    }



}
